// OpenGLReference.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "OpenGLPrimitive.h"
#include "OpenGLInputLoader.h"

vector<COpenGLPrimitivePtr> g_vTriangles;
vector<COpenGLPrimitivePtr> g_vLines;

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_SMOOTH);
}

void triangle(void)
{
   glBegin (GL_TRIANGLES);
   for (unsigned int i = 0; i < g_vTriangles.size(); ++i)
   {
	   for (unsigned int j = 0; j < 3; ++j)
	   {
		   const CVertex& v = g_vTriangles[i]->GetVertex(j);
		   glColor3f(v.color.x, v.color.y, v.color.z);
		   glVertex2f(v.worldPosition.x, v.worldPosition.y);
	   }
   }
   glEnd();

   glBegin (GL_LINES);
   for (unsigned int i = 0; i < g_vLines.size(); ++i)
   {
	   for (unsigned int j = 0; j < 2; ++j)
	   {
		   const CVertex& v = g_vLines[i]->GetVertex(j);
		   glColor3f(v.color.x, v.color.y, v.color.z);
		   glVertex2f(v.worldPosition.x, v.worldPosition.y);
	   }
   }
   glEnd();
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   triangle ();
   glFlush ();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei)w, (GLsizei)h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity ();
   gluOrtho2D (-1.0, 4.0, -1.0, 4.0);
   glMatrixMode(GL_MODELVIEW);
}

bool initData(const char* pFileName)
{
	return OpenGLInputLoader::LoadPrimitives(pFileName, g_vTriangles, g_vLines);
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (WINDOW_WIDTH, WINDOW_HEIGHT); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);

   if (argc < 2)
		cout << "Usage : OpenGLReference [filename]\n" << endl;
   else
   {
	   if (initData(argv[1]))
		   glutMainLoop();
	   else
		   cout << "Invalid input file\n" << endl;
   }
   
   return 0;
}
